import 'package:flutter/material.dart';
import '../../tokens/colors.dart';
import '../../tokens/spacing.dart';
import '../../tokens/borders.dart';
import '../../tokens/shadows.dart';

/// UI组件库卡片样式
/// 提供统一的卡片样式配置
class UICardStyles {
  // 基础卡片样式
  static BoxDecoration basicCard(ColorScheme colorScheme) {
    return BoxDecoration(
      color: colorScheme.surface,
      borderRadius: UIBorders.card,
      boxShadow: UIShadows.cardShadow,
    );
  }

  // 高亮卡片样式
  static BoxDecoration elevatedCard(ColorScheme colorScheme) {
    return BoxDecoration(
      color: colorScheme.surface,
      borderRadius: UIBorders.card,
      boxShadow: UIShadows.cardHoverShadow,
    );
  }

  // 扁平卡片样式
  static BoxDecoration flatCard(ColorScheme colorScheme) {
    return BoxDecoration(
      color: colorScheme.surface,
      borderRadius: UIBorders.card,
      border: Border.all(
        color: colorScheme.outline.withOpacity(0.2),
        width: UIBorders.widthThin,
      ),
    );
  }

  // 填充卡片样式
  static BoxDecoration filledCard(ColorScheme colorScheme) {
    return BoxDecoration(
      color: colorScheme.surfaceVariant.withOpacity(0.3),
      borderRadius: UIBorders.card,
    );
  }

  // 主色调卡片样式
  static BoxDecoration primaryCard(ColorScheme colorScheme) {
    return BoxDecoration(
      color: colorScheme.primaryContainer,
      borderRadius: UIBorders.card,
      boxShadow: UIShadows.cardShadow,
    );
  }

  // 成功状态卡片样式
  static BoxDecoration successCard(ColorScheme colorScheme) {
    return BoxDecoration(
      color: UIColors.successContainer,
      borderRadius: UIBorders.card,
      boxShadow: UIShadows.successShadow,
      border: Border.all(
        color: UIColors.success.withOpacity(0.3),
        width: UIBorders.widthThin,
      ),
    );
  }

  // 错误状态卡片样式
  static BoxDecoration errorCard(ColorScheme colorScheme) {
    return BoxDecoration(
      color: colorScheme.errorContainer,
      borderRadius: UIBorders.card,
      boxShadow: UIShadows.errorShadow,
      border: Border.all(
        color: colorScheme.error.withOpacity(0.3),
        width: UIBorders.widthThin,
      ),
    );
  }

  // 警告状态卡片样式
  static BoxDecoration warningCard(ColorScheme colorScheme) {
    return BoxDecoration(
      color: UIColors.warningContainer,
      borderRadius: UIBorders.card,
      boxShadow: UIShadows.warningShadow,
      border: Border.all(
        color: UIColors.warning.withOpacity(0.3),
        width: UIBorders.widthThin,
      ),
    );
  }

  // 信息状态卡片样式
  static BoxDecoration infoCard(ColorScheme colorScheme) {
    return BoxDecoration(
      color: UIColors.infoContainer,
      borderRadius: UIBorders.card,
      boxShadow: UIShadows.infoShadow,
      border: Border.all(
        color: UIColors.info.withOpacity(0.3),
        width: UIBorders.widthThin,
      ),
    );
  }

  // 小尺寸卡片样式
  static BoxDecoration smallCard(BoxDecoration baseDecoration) {
    return baseDecoration.copyWith(
      borderRadius: UIBorders.cardSmall,
    );
  }

  // 大尺寸卡片样式
  static BoxDecoration largeCard(BoxDecoration baseDecoration) {
    return baseDecoration.copyWith(
      borderRadius: UIBorders.cardLarge,
    );
  }

  // 选中状态卡片样式
  static BoxDecoration selectedCard(ColorScheme colorScheme) {
    return BoxDecoration(
      color: colorScheme.primaryContainer.withOpacity(0.3),
      borderRadius: UIBorders.card,
      border: Border.all(
        color: colorScheme.primary,
        width: UIBorders.widthFocus,
      ),
      boxShadow: UIShadows.cardShadow,
    );
  }

  // 悬停状态卡片样式
  static BoxDecoration hoverCard(ColorScheme colorScheme) {
    return BoxDecoration(
      color: colorScheme.surface,
      borderRadius: UIBorders.card,
      boxShadow: UIShadows.cardHoverShadow,
      border: Border.all(
        color: colorScheme.outline.withOpacity(0.3),
        width: UIBorders.widthThin,
      ),
    );
  }

  // 渐变卡片样式
  static BoxDecoration gradientCard({
    required Gradient gradient,
    BorderRadius? borderRadius,
  }) {
    return BoxDecoration(
      gradient: gradient,
      borderRadius: borderRadius ?? UIBorders.card,
      boxShadow: UIShadows.cardShadow,
    );
  }

  // 透明卡片样式
  static BoxDecoration transparentCard(ColorScheme colorScheme) {
    return BoxDecoration(
      color: colorScheme.surface.withOpacity(0.8),
      borderRadius: UIBorders.card,
      border: Border.all(
        color: colorScheme.outline.withOpacity(0.1),
        width: UIBorders.widthThin,
      ),
    );
  }

  // 图像卡片样式
  static BoxDecoration imageCard(ColorScheme colorScheme) {
    return BoxDecoration(
      color: colorScheme.surface,
      borderRadius: UIBorders.card,
      boxShadow: UIShadows.imageShadow,
    );
  }

  // 列表项卡片样式
  static BoxDecoration listItemCard(ColorScheme colorScheme) {
    return BoxDecoration(
      color: colorScheme.surface,
      borderRadius: UIBorders.md,
      border: Border.all(
        color: colorScheme.outline.withOpacity(0.1),
        width: UIBorders.widthThin,
      ),
    );
  }

  // 紧凑卡片样式
  static BoxDecoration compactCard(ColorScheme colorScheme) {
    return BoxDecoration(
      color: colorScheme.surface,
      borderRadius: UIBorders.sm,
      border: Border.all(
        color: colorScheme.outline.withOpacity(0.2),
        width: UIBorders.widthThin,
      ),
    );
  }

  // 卡片内边距配置
  static EdgeInsets get defaultPadding => const EdgeInsets.all(UISpacing.cardPadding);
  static EdgeInsets get compactPadding => const EdgeInsets.all(UISpacing.buttonPaddingSmall);
  static EdgeInsets get largePadding => const EdgeInsets.all(UISpacing.buttonPaddingLarge);
  
  static EdgeInsets get contentPadding => const EdgeInsets.symmetric(
    horizontal: UISpacing.cardPadding,
    vertical: UISpacing.buttonPaddingSmall,
  );

  // 卡片外边距配置
  static EdgeInsets get defaultMargin => const EdgeInsets.all(UISpacing.cardMargin);
  static EdgeInsets get verticalMargin => const EdgeInsets.symmetric(
    vertical: UISpacing.cardMargin,
  );
  static EdgeInsets get horizontalMargin => const EdgeInsets.symmetric(
    horizontal: UISpacing.cardMargin,
  );

  // 卡片头部样式
  static BoxDecoration cardHeader(ColorScheme colorScheme) {
    return BoxDecoration(
      color: colorScheme.surfaceVariant.withOpacity(0.1),
      borderRadius: const BorderRadius.vertical(
        top: Radius.circular(UIBorders.radiusLG),
      ),
      border: Border(
        bottom: BorderSide(
          color: colorScheme.outline.withOpacity(0.2),
          width: UIBorders.widthThin,
        ),
      ),
    );
  }

  // 卡片底部样式
  static BoxDecoration cardFooter(ColorScheme colorScheme) {
    return BoxDecoration(
      color: colorScheme.surfaceVariant.withOpacity(0.1),
      borderRadius: const BorderRadius.vertical(
        bottom: Radius.circular(UIBorders.radiusLG),
      ),
      border: Border(
        top: BorderSide(
          color: colorScheme.outline.withOpacity(0.2),
          width: UIBorders.widthThin,
        ),
      ),
    );
  }

  // 获取卡片样式工厂方法
  static BoxDecoration getCardStyle({
    required String type,
    required ColorScheme colorScheme,
    String size = 'medium',
    bool isSelected = false,
    bool isHover = false,
    Gradient? gradient,
  }) {
    BoxDecoration baseDecoration;

    // 处理状态
    if (isSelected) {
      return selectedCard(colorScheme);
    }
    if (isHover) {
      return hoverCard(colorScheme);
    }

    // 处理渐变
    if (gradient != null) {
      return gradientCard(gradient: gradient);
    }

    // 基础样式
    switch (type.toLowerCase()) {
      case 'elevated':
        baseDecoration = elevatedCard(colorScheme);
        break;
      case 'flat':
        baseDecoration = flatCard(colorScheme);
        break;
      case 'filled':
        baseDecoration = filledCard(colorScheme);
        break;
      case 'primary':
        baseDecoration = primaryCard(colorScheme);
        break;
      case 'success':
        baseDecoration = successCard(colorScheme);
        break;
      case 'error':
        baseDecoration = errorCard(colorScheme);
        break;
      case 'warning':
        baseDecoration = warningCard(colorScheme);
        break;
      case 'info':
        baseDecoration = infoCard(colorScheme);
        break;
      case 'transparent':
        baseDecoration = transparentCard(colorScheme);
        break;
      case 'image':
        baseDecoration = imageCard(colorScheme);
        break;
      case 'listitem':
        baseDecoration = listItemCard(colorScheme);
        break;
      case 'compact':
        return compactCard(colorScheme);
      default:
        baseDecoration = basicCard(colorScheme);
    }

    // 处理尺寸
    switch (size.toLowerCase()) {
      case 'small':
        return smallCard(baseDecoration);
      case 'large':
        return largeCard(baseDecoration);
      default:
        return baseDecoration;
    }
  }
}